<template>
  <base-breadcrumb :title="title">
    <!-- 评分处理 -->
    <a-form-model
      ref="ruleForm"
      :model="formInfo"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 18 }"
      :rules="rules"
      id="base-breadcrumb"
      style="position: relative"
    >
      <a-card :bordered="false">
        <title-name title="处理"></title-name>
        <a-card :bordered="false">
          <title-name :title="noticeType === '302' ? '问题解决型课题资料评审表' : '创新型课题资料评审表'"></title-name>
          <div class="mb20">
            <a-table
              :loading="loading"
              bordered
              rowKey="id"
              :pagination="false"
              :columns="columns"
              :data-source="tableData"
            >
              <span
                slot="serial"
                slot-scope="text, record, index"
              >{{ index + 1 }}</span>
              <!-- 评分 -->
              <template
                slot="score"
                slot-scope="text, record"
              >
                <template v-if="resolveInfo">
                  {{ record.score }}
                </template>
                <BaseInputNumber
                  v-else
                  v-model="record.score"
                  placeholder="请评分"
                  :min="0"
                  :max="Number(record[columns.find((i) => i.code === '4').dataIndex] || 0)"
                  :precision="0"
                />
              </template>
            </a-table>
            <a-row>
              <a-col :span="8">
                <a-form-model-item
                  label="评审总分"
                  :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                  :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
                >
                  <BaseInput
                    :value="totalScore"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item
                  label="评审信息"
                  :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                  :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
                  prop="reviewInformation"
                >
                  <BaseTextarea
                    :disabled="resolveInfo"
                    :maxLength="3000"
                    :autoSize="{ minRows: 16, maxRows: 32 }"
                    placeholder="请输入评审信息"
                    v-model="formInfo.reviewInformation"
                  />
                </a-form-model-item>
              </a-col>
            </a-row>
          </div>

          <!-- 页脚操作栏 -->
          <div class="page-btn-right-top">
            <a-button
              v-if="resolveInfo"
              @click="$close"
            >
              关闭
            </a-button>
            <a-button
              v-else
              type="primary"
              @click="handleSubmit"
            >确认处理</a-button>
          </div>
          <div
            class="mb20"
            style="font-size: 30px; font-weight: bolder"
          >业务详情</div>
          <!-- 基本信息 -->
          <title-name title="基本信息"></title-name>
          <a-spin :spinning="loading2">
            <custom-form
              :formList="formList"
              :isEmit="false"
              :isValidate="true"
              :form="form"
              editModel="V"
            >
            </custom-form>
            <title-name title="相关附件" />
            <BTableUpload
              v-model="form.files"
              :disabled="true"
              businessCode="QM12"
            />
          </a-spin>
        </a-card>
      </a-card>
    </a-form-model>
  </base-breadcrumb>
</template>

<script>
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import { finishBusinessTodo } from '@/api/commons.js'
import { getHeaders, getContent } from '@/api/commons'
import { qcActivityAchievementReview } from '@/api/quality/quality' // api

export default {
  name: 'a' + Date.now(),
  extends: new BaseEditPage(),
  data() {
    return {
      loading: false, // 表格加载loading
      loading2: false, // 详情加载loading
      form: {}, // 详情表单
      formInfo: {}, // 提交表单
      columns: [], // 评价表头
      tableData: [], //评审列表
      // 表单列表
      formList: [
        {
          keyName: 'branchComId',
          label: '所属分公司',
          viewName: 'branchComName'
        },
        {
          keyName: 'projectId',
          label: '所属项目',
          viewName: 'projectName'
        },
        {
          keyName: 'activeYear',
          label: '活动开展年度'
        },
        {
          keyName: 'actualStartTime',
          label: '实际开始时间'
        },
        {
          keyName: 'actualEndTime',
          label: '实际结束时间'
        },
        {
          keyName: 'completeEmployeeName',
          label: '课题实际完成人'
        },
        {
          keyName: 'topicName',
          label: '课题名称'
        },
        {
          keyName: 'topicRegisterNumber',
          label: '课题注册号'
        },
        {
          keyName: 'topicTypeName',
          label: '课题类型'
        },
        {
          keyName: 'topicGroupCreationTime',
          label: '小组组建日期'
        },
        {
          keyName: 'trainTime',
          label: '质量管理小组人均培训时间'
        },
        {
          keyName: 'guidanceTypeName',
          label: '指导者'
        },
        {
          keyName: 'dutyName',
          label: '课题负责人'
        },
        {
          keyName: 'departmentName',
          label: '质量管理部门'
        }
      ],
      rules: {
        reviewInformation: [{ required: true, whitespace: true, message: '请输入评审信息', trigger: 'change' }]
      } // 表单校验规则
    }
  },
  mounted() {
    this.typeId = this.noticeType === '302' ? '634319726755057666' : '634319726755057665'
    this.init()
  },
  computed: {
    // 是否是业务待办查看处理信息
    resolveInfo: {
      get() {
        return this.$route.query?.resolveInfo === 'true'
      },
      set() {}
    },
    // 总分
    totalScore() {
      let datas = this.tableData.filter((i) => !!i.score)
      let numbers = datas.map((i) => i.score)
      return numbers.length ? numbers.reduce((t, i) => Number(t || 0) + Number(i || 0)) : 0
    }
  },
  methods: {
    /**
     * 初始化
     */
    init() {
      this.getHeaders()
      this.getContent()
      this.getDetail()
    },
    /**
     * 获取详情
     */
    getDetail() {
      this.loading2 = true
      qcActivityAchievementReview
        .noticeDetail({ id: this.id })
        .then((res) => {
          this.loading2 = false
          if (res.code === 200) {
            // 若文件不存在则设置空数组
            !res.data.files && (res.data.files = [])
            // 若注册信息不存在则设置空对象
            if (res.data.register) {
              const formNames = ['topicName', 'topicTypeName', 'topicGroupCreationTime', 'trainTime', 'guidanceTypeName', 'dutyName', 'topicRegisterNumber']
              formNames.forEach((i) => {
                res.data[i] = res.data.register[i]
              })
              res.data.registerId = res.data.register.id
              delete res.data.register
            }
            res.data.departmentName = '技术质量部'
            this.form = res.data
          }
        })
        .catch(() => {
          this.loading2 = false
        })
    },
    /**
     * 获取评价表内容
     */
    async getContent() {
      this.loading = true
      let res = await getContent({ formId: this.typeId }).catch(() => {
        this.loading = false
      })
      this.tableData = res.data
      this.loading = false
    },
    /**
     * 获取评价表表头
     */
    getHeaders() {
      this.loading = true
      getHeaders({ formId: this.typeId })
        .then((res) => {
          this.loading = false
          if (res.code === 200) {
            res.data?.splice(res.data.length - 1, 1)
            res.data?.forEach((item) => (item.ellipsis = true))
            res.data?.push({
              dataIndex: 'score',
              formId: null,
              title: '评分',
              code: '0',
              sort: 4,
              scopedSlots: { customRender: 'score' }
            })
            this.columns = res.data
            let obj = {
              dataIndex: 'serial',
              formId: null,
              title: '序号',
              code: '0',
              sort: 0,
              inputType: null,
              scopedSlots: { customRender: 'serial' }
            }
            this.columns.unshift(obj)
          }
        })
        .catch(() => {
          this.loading = false
        })
    },
    /**
     * 提交表单
     */
    handleSubmit() {
      let that = this
      if (!that.tableData.length || that.tableData.some((i) => !i.score && i.score != 0)) {
        that.$message.warning('请完成评分再提交！')
        return
      }
      // 校验表单
      that.$refs.ruleForm.validate((e) => {
        if (e) {
          that.$confirm({
            title: '温馨提示',
            content: '确定提交，提交后不可撤回？',
            onOk: () => {
              return qcActivityAchievementReview
                .handleScore({
                  reviewInformation: this.formInfo.reviewInformation,
                  totalScore: this.totalScore,
                  personId: this.$userInfo().id,
                  id: this.id
                })
                .then((res) => {
                  if (res.data.code === 200) {
                    return finishBusinessTodo({
                      id: this.messageId
                    }).then((res) => {
                      if (res.code == '0') {
                        that.$message.success('提交成功！')
                        this.$close()
                      }
                    })
                  }
                })
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .ant-calendar-picker {
  width: 100% !important;
}
</style>
